﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Core.Algorithm;
using Microhard.Core.DBUtility;

namespace Microhard.Permission.MVCFilter
{
    public class BeforeDeleteSingleModelActionFilter : IActionDataFilter
    {
        #region IActionDataFilter Members

        public bool FilterAction(System.Web.Mvc.ActionExecutingContext filterContext, Models.User currentUser, List<Models.Permission> currentPermissions, Core.DBUtility.ScopeResult dataScope)
        {
            var model = filterContext.ActionParameters["model"];
            Guid id = AlgCompare.GetPKID<ModelPKIDAttribute>(model);
            this.PKID = id;
            if (dataScope.Scope == DataCollectionScope.Custome ||
                IsAdminAction || dataScope.PKID.Contains(id))
            {
                var delbpm = model as BasePermissionModel;
                if (delbpm != null)
                {
                    delbpm.OperationStatus = OperationStatus.Delete;
                }
                filterContext.ActionParameters["model"] = model;
                return true;
            }
            return false;
        }

        public Guid PKID
        {
            set;
            get;
        }
        #endregion

        public bool IsAdminAction
        {
            set;
            get;
        }
    }
}
